/**
 * 抓取远端API的结构
 * https://developers.douban.com/wiki/?title=movie_v2
 * @param  {String} api    api 根地址
 * @param  {String} path   请求路径
 * @param  {Objece} params 参数
 * @return {Promise}       包含抓取任务的Promise
 */
function request(url, params) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      data: Object.assign({}, params),
      method: 'POST', //default: GET，value：OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        resolve(res.data)
      },
      fail: reject
    })
  })
}



// //选取图片
// function uploadImage(res) {
//   wx.chooseImage({
//     count: 9,
//     sizeType: ['original', 'compressed'],
//     sourceType: ['album', 'camera'],
//     success: (res) => {
//       var successUp = 0; //成功个数
//       var failUp = 0; //失败个数
//       var length = res.tempFilePaths.length; //总共个数
//       var i = 0; //第几个
//       this.uploadFiles(res.tempFilePaths, successUp, failUp, i, length);
//     },
//   });
// }

/* 函数描述：作为上传文件时递归上传的函数体体；
 * 参数描述： 
 * tempFilePaths
 * successUp是成功上传的个数
 * failUp是上传失败的个数
 * i是文件路径数组的指标
 * length是文件路径数组的长度
 */
var filesRes //全部局存储请求成功后的回调数据
function uploadFiles(url, params, tempFilePaths, successUp, failUp, position, length, doSuccess) {
  return new Promise((resolve, reject) => {
    if (position == 0) {
      filesRes = []
    }

    //for循环方法
    for (var i = 0; i < tempFilePaths.length; i++) {
      wx.uploadFile({
        method: 'POST',
        url: url,
        filePath: tempFilePaths[i],
        name: 'fileName',
        header: {
          'content-type': 'multipart/form-data'
        },
        formData: Object.assign({}, params),
        success: (res) => {
          console.log(res.data)
          let files = JSON.parse(res.data);
          filesRes.push(files.data)
          successUp++;
        },
        fail: (res) => {
          console.log(res)
          failUp++;
        },
        complete: () => {
          position++;
          if (position == length) {
            console.log('总共' + successUp + '张上传成功,' + failUp + '张上传失败！');
            if (successUp > 0 && failUp == 0) {
              resolve(filesRes)
            } else {
              reject
            }
          }
        },
      });
    }

    //递归方法
    // wx.uploadFile({
    //   method: 'POST',
    //   url: url,
    //   filePath: tempFilePaths[i],
    //   name: 'fileName',
    //   header: {
    //     'content-type': 'multipart/form-data'
    //   },
    //   formData: Object.assign({}, params),
    //   success: (res) => {
    //     console.log(res)
    //     filesRes.push(res)
    //     successUp++;
    //   },
    //   fail: (res) => {
    //     console.log(res)
    //     failUp++;
    //   },
    //   complete: () => {
    //     i++;
    //     if (i == length) {
    //       console.log('总共' + successUp + '张上传成功,' + failUp + '张上传失败！');
    //       if (typeof doSuccess == "function") {
    //         doSuccess(filesRes)
    //       }
    //     } else { //递归调用uploadFiles函数
    //       this.uploadFiles(url, params, tempFilePaths, successUp, failUp, i, length, doSuccess);
    //     }
    //   },
    // });

  })
}

module.exports = {
  request,
  uploadFiles
}